﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;
public partial class Products_Default : ClassBase
{
    string InClassKey = "";
    string CommodKey = "";
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        InClassKey = Globals.GetStringFromQueryString("InClassKey");
        CommodKey = Globals.GetStringFromQueryString("CommodKey");     
        Load_Title();        
        Pager.IndexChanged += new EventHandler(PageIndex_Changed);
        
        if (!Page.IsPostBack)
        {
            try
            {
                Session["BackPageUrl"] = Request.Url.AbsoluteUri;
            }
            catch
            {

            }
            this.DataBind();
           
        }
    }

    private void PageIndex_Changed(Object sender, EventArgs e)
    {
        DataBind();
    }


    private void Load_Title()
    {
        Pager.PageSize = 3;
        if ( CommodKey !="")
        {
            
            // kiem tra xem can hien thi san pham hinh thuc nao
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);            
            // xem loai cha
            if (obj.Count > 0)
            {
                Title.TitleName = InClassCategory.GetNameCategoryByCommodKey(CommodKey, ref error);
                ProductByParent.Visible = true;                
                Panel_ProductByCate.Visible = false;
                Pager.Visible = false;
            }
            else // Commod chỉ có item
            {
                Title.TitleName = Title.TitleName = InClassCategory.GetNameCategoryByCommodKey(CommodKey, ref error);

                ProductByParent.Visible = false;                
                Panel_ProductByCate.Visible = true;
                Pager.Visible = true;
            }
        }
        else if (InClassKey != "")
        {
            Title.TitleName = InClassCategory.GetCommodNameCategoryByInClassKey(InClassKey, ref error) + " <img src=" + Globals.ImagesUrl() + "li1.gif align='absmiddle'> " + InClassCategory.GetNameCategoryByInClassKey(InClassKey, ref error);

            ProductByParent.Visible = false;
            Panel_ProductByCate.Visible = true;
            Pager.Visible = true;
        }
        else
        {
            Title.TitleName = ResourceManager.GetString("Product_Category");
            ProductByParent.Visible = false;
            Panel_ProductByCate.Visible = false;
            Pager.Visible = false;
        }
    }


    protected string GetPrice(float UnitPrice)
    {
        if (UnitPrice > 0)
            return UnitPrice.ToString() + " VNĐ"; //Formatter.FormatMoney(UnitPrice)
        else
            return "Liên hệ"; //ResourceManager.GetString("Product_Price_Lienhe");
    }


    public override void DataBind()
    {
        // hien thi bao nhieu hinh tren 1 trang
        if (CommodKey != "")
        {
            List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);
            // xem loai cha
            if (obj.Count <=0)
            {
                Pager.PageSize = 12;
                ///comkey 
                List<Products> objl = Products.GetProductsByCommodKey(CommodKey, Pager.PageIndex, Pager.PageSize, ref error);
                ItemsDataList.DataSource = objl;
                ItemsDataList.DataBind();

                Pager.TotalRecords = Products.GetProductsCountByCommodKey(CommodKey, ref error);

                // kiem tra xem co can hien thi phan trang ko
                if (Pager.TotalRecords <= 1)
                    DisplayPager.Visible = false;
                else
                    DisplayPager.Visible = true;
            }
        }
        else
        {
            Pager.PageSize = 12;

            List<Products> obj = Products.GetProductsByInClassKey(InClassKey, Pager.PageIndex, Pager.PageSize, ref error);
            ItemsDataList.DataSource = obj;
            ItemsDataList.DataBind();

            Pager.TotalRecords = Products.GetProductsCountByInClassKey(InClassKey, ref error);

            // kiem tra xem co can hien thi phan trang ko
            if (Pager.TotalRecords <= 1)
                DisplayPager.Visible = false;
            else
                DisplayPager.Visible = true;
        }
    }

    protected string GetButtonAddToCart(Double UnitPrice)
    {
        if (UnitPrice > 0)
            //if (Globals.LangVietNam())
                return "<img src=" + Globals.ImagesUrl() + "AddToCart_Nho.gif border=0 />";
            //else
            //    return "<img src=" + Globals.ImagesUrl() + "AddToCart_Nho_en.gif border=0 />";
        return "";
    }

   protected void ItemsDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {        

        Image img = (Image)e.Item.FindControl("testImage");

        Label lblPrice = (Label)e.Item.FindControl("lblPrice");
        if (lblPrice.Text == "Liên hệ")
        {
            img.Visible = false;
        }
        else
        {
            HtmlInputHidden txtItemkey = (HtmlInputHidden)e.Item.FindControl("txtItemkey");
            string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");

            img.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);
            img.ImageUrl = Globals.ImagesUrl() + "AddToCart_nho.gif";
        }
         
    }   

}